using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        private NSD nsd;
        private NsdDAO nsdService;

        protected void Page_Load(object sender, EventArgs e)
        {
            nsdService = new NsdDAO();
            nsd = new NSD();
            try
            {
                nsd = (NSD)Session["nsd"];
                if (nsd == null)
                {
                    try
                    {
                        if (Request.Cookies["userInfo"] != null)
                        {
                            Request.Cookies["userInfo"].Expires = DateTime.Now.AddMinutes(60);
                            Response.Cookies.Add(Request.Cookies["userInfo"]);
                            string userName = Request.Cookies["userInfo"]["userName"];
                            nsd = nsdService.Get(userName);
                            Session["nsd"] = nsd;
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.Show(ex.Message, Request.UrlReferrer.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Show(ex.Message, Request.UrlReferrer.ToString());
            }
            if (!IsPostBack)
            {
                if (nsd != null)
                {
                    Response.Redirect("TrangChuQuanTri.aspx");
                }
            }
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            NSD nsd = nsdService.Get(TextBoxUserName.Text);
            if (nsd != null && nsd.Matkhau==Common.HashMD5(TextBoxPassword.Text))
            
            //if (nsd != null && nsd.Matkhau.Equals(Common.HashMD5(TextBoxPassword.Text)))
            // Dang nhap Thanh cong
            {
                Session["nsd"] = nsd;

                HttpCookie aCookie = new HttpCookie("userInfo");
                aCookie.Values["userName"] = nsd.Tendangnhap;
                aCookie.Expires = CheckBoxRemenberMe.Checked ? DateTime.Now.AddDays(180) : DateTime.Now.AddMinutes(60);
                Response.Cookies.Add(aCookie);

                Response.Redirect("TrangChuQuanTri.aspx");
            }
            else Common.Show("Đăng nhập không thành công, xem lại tệ và mật khẩu!", "Login.aspx");
        }

        protected void ButtonBackHomePage_Click(object sender, EventArgs e)
        {
            Response.Redirect("../TrangChu.aspx");
        }
    }
}
